//axios基础封装
import axios from 'axios'
import {ElMessage} from 'element-plus'
import {useRouter} from 'vue-router'
const router = useRouter();
const request = axios.create({
  baseURL: 'http://pcapi-xiaotuxian-front-devtest.itheima.net',
  timeout: 5000
})

//拦截器
request.interceptors.request.use(config => {
  // let userStore = useUserStore()
  // let token = userStore.userInfo?.token
  // if (token) {
  //   config.headers.Authorization = `Bearer ${token}`
  // }
  return config
}, error => Promise.reject(error))

request.interceptors.response.use(res => {
    return res.data
  }
  , e => {
    // let userStore = useUserStore()
    ElMessage({type: 'error', message: e.response.data.msg})
    // if(e.response.status===401){
    //   userStore.clearUserInfo();
    //   router.push('/login')
    // }
    return Promise.reject(e)
  })


export default request
